import React, { useEffect, useState } from 'react'
import { Redirect, Route, Switch } from 'react-router-dom'
import UserList from '../../views/sandbox/user-manage/UserList'
import RoleList from '../../views/sandbox/right-manage/RoleList'
import RightList from '../../views/sandbox/right-manage/RightList'
import Home from '../../views/sandbox/home/Home'
import NoPermission from '../../views/sandbox/nopermission/NoPermission'
import NewsAdd from '../../views/sandbox/news-manage/NewsAdd'
import NewsDraft from '../../views/sandbox/news-manage/NewsDraft'
import NewsCategory from '../../views/sandbox/news-manage/NewsCategory'
import NewsPreview from '../../views/sandbox/news-manage/NewsPreview'
import NewsUpdate from '../../views/sandbox/news-manage/NewsUpdate'
import Audit from '../../views/sandbox/audit-manage/Audit'
import AuditList from '../../views/sandbox/audit-manage/AuditList'
import Unpublished from '../../views/sandbox/publish-manage/Unpublished'
import Published from '../../views/sandbox/publish-manage/Published'
import Sunset from '../../views/sandbox/publish-manage/Sunset'
import axios from 'axios'
import { Spin } from 'antd'
import { connect } from 'react-redux'
// import LoadingReducer from '../../redux/reducers'

const LocalRouterMap = {
    "/home": Home,
    // "/user-manage":
    // "/user-manage/add":
    // "/user-manage/delete":
    // "/user-manage/update":
    "/user-manage/list": UserList,
    // "/right-manage":
    "/right-manage/role/list": RoleList,
    "/right-manage/right/list": RightList,
    // "/right-manage/role/update":
    // "/right-manage/role/delete":
    // "/right-manage/right/update":
    // "/right-manage/right/delete":
    // "/news-manage":
    // "/news-manage/list":
    "/news-manage/add": NewsAdd,
    "/news-manage/update/:id": NewsUpdate,
    "/news-manage/preview/:id": NewsPreview,
    "/news-manage/draft": NewsDraft,
    "/news-manage/category": NewsCategory,
    // "/audit-manage":
    "/audit-manage/audit": Audit,
    "/audit-manage/list": AuditList,
    // "/publish-manage":
    "/publish-manage/unpublished": Unpublished,
    "/publish-manage/published": Published,
    "/publish-manage/sunset": Sunset,
}

function NewsRouter(props) {
    const token = localStorage.getItem("token")
    // 一个简单的抛出异常，可以在脚本任何地方，会被 `onError` 捕获
    try {
        JSON.parse(token)
    } catch (e) {
        console.log(e);
        localStorage.removeItem("token")
        props.history.push("/login")
    }

    const { role: { rights } } = JSON.parse(token)

    const [BackRouteList, setBackRouteList] = useState([])
    useEffect(() => {
        Promise.all([
            axios.get("/rights"),
            axios.get("/children")
        ]).then(res => {
            setBackRouteList([...res[0].data, ...res[1].data])
        })
    }, [])

    const checkRoute = (item) => {
        return LocalRouterMap[item.key] != undefined && (item.pagepermisson === 1 || item.routepermisson === 1)
    }
    const checkUserPermission = (item) => {
        return rights.includes(item.key)
    }

    return (
        <Spin tip="Loading..." size="large"
            spinning={props.isLoading}
            delay={3}>
            <Switch>
                {/* <Route path='/home' component={Home}></Route>
            <Route path='/user-manage/list' component={UserList}></Route>
            <Route path='/right-manage/role/list' component={RoleList}></Route>
            <Route path='/right-manage/right/list' component={RightList}></Route> */}
                {
                    BackRouteList.map(item => {
                        if (checkRoute(item) && checkUserPermission(item)) {
                            return <Route path={item.key} key={item.key} component={LocalRouterMap[item.key]} exact />
                        }
                        return null
                    }
                    )
                }
                {/* 默认进入home页面 */}
                <Redirect from='/' to='/home' exact></Redirect>
                {/* 进入403页面 */}
                {BackRouteList.length > 0 && <Route path='*' component={NoPermission}></Route>}
            </Switch>
        </Spin>
    )
}

const mapStateToProps = ({ LoadingReducer: { isLoading } }) => ({
    isLoading
})
export default connect(mapStateToProps)(NewsRouter)